package com.fcml.pattern.observer.subject;

import com.fcml.pattern.observer.observer.Observer;

import java.util.HashSet;

/**
 * @author fcml
 * @date 2018/11/3
 *
 * 微信公众号主题
 */
public class WeChatSubject implements Subject {
    /*保存订阅此主题的观察者集合*/
    private HashSet<Observer> observers;
    /*公众号推送的消息*/
    private String message;

    public WeChatSubject(HashSet<Observer> observers) {
        this.observers = observers;
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if (observers.size() != 0) {
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        observers.forEach(observer -> observer.notify(message));
    }

    public void setPublishMessage(String message) {
        this.message = message;
        System.out.println("微信服务更新消息： " + message);
        notifyObservers();  //通知所有观察者
    }
}
